import 'dart:convert';

import 'package:flutter_module/common/utils/object_to_any.dart';
import 'package:flutter_module/model/service_provider_audit_state.dart';

class ServiceProviderDetailModel {
  ServiceProviderDetailModel({
    required this.preferential,
    required this.videoUrl,
    required this.agreementId,
    required this.bossTelephone,
    required this.id,
    required this.creditCode,
    required this.caseList,
    required this.address,
    required this.businessLicenceUrl,
    required this.companySize,
    required this.collectCount,
    required this.bossWeChat,
    required this.district,
    required this.targetCodeName,
    required this.actualPageView,
    required this.serviceScope,
    required this.companySizeName,
    required this.editRefuseReason,
    required this.editStatus,
    required this.companyLogoUrl,
    required this.kvImage,
    required this.bossEmail,
    required this.city,
    required this.companyId,
    required this.companyProfile,
    required this.refuseReason,
    required this.status,
    required this.province,
    required this.collectFlag,
    required this.skuId,
    required this.bossName,
    required this.targetCode,
    required this.companyName,
    required this.userId,
    required this.videoImage,
    required this.provinceName,
  });

  /// 优惠政策
  String preferential;

  /// 视频URL
  String videoUrl;

  /// 视频封面
  String videoImage;
  int agreementId;
  String bossTelephone;
  int id;
  String creditCode;

  /// 案例列表
  List<ServiceProviderCaseModel> caseList;
  String address;
  String businessLicenceUrl;

  /// 收藏次数
  int collectCount;
  String bossWeChat;
  int district;

  /// 服务对象
  List<String> targetCodeName;
  int actualPageView;

  /// 服务范围
  List<String> serviceScope;

  /// 编辑失败原因
  String editRefuseReason;

  /// 合作服务商审核状态 0-未审核 1-审核中 2-审核拒绝
  ServiceProviderAuditState editStatus;

  /// brand 图
  List<String> kvImage;
  String bossEmail;
  int city;

  /// 公司简介
  String companyProfile;

  /// 公司名称
  String companyName;

  /// 公司logo
  String companyLogoUrl;
  String companySizeName;
  String companySize;
  int companyId;

  String refuseReason;
  int status;
  int province;

  /// 地域（省份）
  String provinceName;

  /// 是否收藏
  int collectFlag;
  int skuId;
  String bossName;
  List<String> targetCode;

  int userId;

  factory ServiceProviderDetailModel.fromJson(String str) =>
      ServiceProviderDetailModel.fromMap(json.decode(str));

  String toJson() => json.encode(toMap());

  factory ServiceProviderDetailModel.fromMap(Map<String, dynamic> json) =>
      ServiceProviderDetailModel(
        preferential: object2string(json["preferential"]),
        videoUrl: object2string(json["videoUrl"]),
        agreementId: object2int(json["agreementId"]),
        bossTelephone: object2string(json["bossTelephone"]),
        id: object2int(json["id"]),
        creditCode: object2string(json["creditCode"]),
        caseList: object2list(json["caseList"])
            .map((e) => ServiceProviderCaseModel.fromMap(e))
            .toList(),
        address: object2string(json["address"]),
        businessLicenceUrl: object2string(json["businessLicenceUrl"]),
        companySize: object2string(json["companySize"]),
        collectCount: object2int(json["collectCount"]),
        bossWeChat: object2string(json["bossWeChat"]),
        district: object2int(json["district"]),
        targetCodeName: object2string(json["targetCodeName"])
            .split(',')
            .where((e) => e.isNotEmpty)
            .toList(),
        actualPageView: object2int(json["actualPageView"]),
        serviceScope: object2string(json["serviceScope"])
            .split(',')
            .where((e) => e.isNotEmpty)
            .toList(),
        companySizeName: object2string(json["companySizeName"]),
        editRefuseReason: object2string(json["editRefuseReason"]),
        editStatus:
            ServiceProviderAuditState.values[object2int(json["editStatus"])],
        companyLogoUrl: object2string(json["companyLogoUrl"]),
        kvImage: object2string(json["kvImage"])
            .split(',')
            .where((e) => e.isNotEmpty)
            .toList(),
        bossEmail: object2string(json["bossEmail"]),
        city: object2int(json["city"]),
        companyId: object2int(json["companyId"]),
        companyProfile: object2string(json["companyProfile"]),
        refuseReason: object2string(json["refuseReason"]),
        status: object2int(json["status"]),
        province: object2int(json["province"]),
        collectFlag: object2int(json["collectFlag"]),
        skuId: object2int(json["skuId"]),
        bossName: object2string(json["bossName"]),
        targetCode: object2string(json["targetCode"])
            .split(',')
            .where((e) => e.isNotEmpty)
            .toList(),
        companyName: object2string(json["companyName"]),
        userId: object2int(json["userId"]),
        videoImage: object2string(json["videoImage"]),
        provinceName: object2string(json['provinceName']),
      );

  Map<String, dynamic> toMap() => {
        "preferential": preferential,
        "videoUrl": videoUrl,
        "agreementId": agreementId,
        "bossTelephone": bossTelephone,
        "id": id,
        "creditCode": creditCode,
        "caseList": List<dynamic>.from(caseList.map((x) => x.toMap())),
        "address": address,
        "businessLicenceUrl": businessLicenceUrl,
        "companySize": companySize,
        "collectCount": collectCount,
        "bossWeChat": bossWeChat,
        "district": district,
        "targetCodeName": targetCodeName.join(','),
        "actualPageView": actualPageView,
        "serviceScope": serviceScope.join(','),
        "companySizeName": companySizeName,
        "editRefuseReason": editRefuseReason,
        "editStatus": editStatus.index,
        "companyLogoUrl": companyLogoUrl,
        "kvImage": kvImage.join(','),
        "bossEmail": bossEmail,
        "city": city,
        "companyId": companyId,
        "companyProfile": companyProfile,
        "refuseReason": refuseReason,
        "status": status,
        "province": province,
        "collectFlag": collectFlag,
        "skuId": skuId,
        "bossName": bossName,
        "targetCode": targetCode.join(','),
        "companyName": companyName,
        "userId": userId,
        "videoImage": videoImage,
        'provinceName': provinceName,
      };
}

class ServiceProviderCaseModel {
  ServiceProviderCaseModel({
    required this.status,
    required this.id,
    required this.title,
    required this.companyId,
    required this.content,
    required this.images,
  });

  int status;
  int id;

  /// 案例标题
  String title;
  int companyId;

  /// 案例描述
  String content;

  /// 案例图片
  List<String> images;

  factory ServiceProviderCaseModel.fromJson(String str) =>
      ServiceProviderCaseModel.fromMap(json.decode(str));

  String toJson() => json.encode(toMap());

  factory ServiceProviderCaseModel.fromMap(Map<String, dynamic> json) =>
      ServiceProviderCaseModel(
        status: object2int(json["status"]),
        id: object2int(json["id"]),
        title: object2string(json["title"]),
        companyId: object2int(json["companyId"]),
        content: object2string(json["content"]),
        images: object2string(json["images"]).split(','),
      );

  Map<String, dynamic> toMap({bool returnOnlyKeyFields = false}) {
    var maps = Map<String, dynamic>();
    maps.addAll({
      "title": title,
      "content": content,
      "images": images.join(','),
    });
    if (!returnOnlyKeyFields) {
      maps.addAll({
        "status": status,
        "id": id,
        "companyId": companyId,
      });
    }
    return maps;
  }
}
